﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VoteServer.Member;

namespace VoteServer
{
    /// <summary>
    /// 对数据的曾删查改。不想写了
    /// 使用编号匹配查询
    /// </summary>
   
    public class DataServer
    {
        private ProblemServer problemServer { get; set; }//功能类---修改一个问题内容
        private FileServer fileSever;

        private List<User> userList;
        private List<Problem> problemList;

        /*public DataServer()
        {
            voteData = new VoteData();
            fileSever = new FileServer();
            problemList = voteData.GetProblemList();
        }*/

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Problem QueryProblem(int id) {

            VoteData voteData = new VoteServer.VoteData();
            problemList=voteData.GetProblemList();
            Problem problem=problemList.Find(e => e.Id == id);

            return problem;//没有就返回null
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool IsDeleteProblem(int id) {

            VoteData voteData = new VoteData();

            Problem problem= QueryProblem(id);
            if ( problem== null) {
                return false;
            }
            problemList.Remove(problem);

            fileSever.WriteFile_Problem(problemList);
            problemList=voteData.GetProblemList();
            return true;
        }
        /// <summary>
        /// 增
        /// </summary>
        /// <param name="problem"></param>
        /// <returns></returns>
        public bool IsAddPrpblem(Problem problem) {

            VoteData voteData = new VoteData();
            problemList = voteData.GetProblemList();
            if (problemList.Find(e => e.Id==problem.Id) == null)
            {
                problemList.Add(problem);
                fileSever.WriteFile_Problem(problemList);
            }
            else
            {
                return false;
            }

            return true;
        }

        public bool IsAddUser(User user) {
            VoteData voteData = new VoteData();
            userList = voteData.GetUserList();
            if (userList.Find(e=>e.AccountName==user.AccountName)==null) {
                userList.Add(user);
                fileSever.WriteFile_Users(userList);
            }
            else
            {
                return false;
            }

            return true;
        }

    }
}